package com.example.android.BluetoothChat.util.provider.player.impl;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.example.android.BluetoothChat.activity.PlayerListActivity;
import com.example.android.BluetoothChat.bean.Game;
import com.example.android.BluetoothChat.bean.GameRequestResponse;
import com.example.android.BluetoothChat.bean.Player;
import com.example.android.BluetoothChat.constants.EnumPlayerType;

public class BluetoothPlayerProviderImpl extends APlayerProviderImpl {

	private BluetoothAdapter _bluetoothAdapter = null;
	private BroadcastReceiver _broadcastReceiver;
	
	@Override
	public void findPlayersNearby(final Context parContext) {
		if(PlayerListActivity.D) Log.i(getClass().getSimpleName(), "finding players nearby with bluetooth");

		// The BroadcastReceiver that listens for discovered devices and
		// changes the title when discovery is finished


		if(_bluetoothAdapter == null){
			
			_bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
			
			if(_bluetoothAdapter == null){
				if(PlayerListActivity.D) Log.i(getClass().getSimpleName(), "Bluetooth not supported. Aborting bluetooth player discovery");

				return;
			} 
		}
		if(!_bluetoothAdapter.isEnabled()){
			if(PlayerListActivity.D) Log.i(getClass().getSimpleName(), "Bluetooth not enabled. Aborting bluetooth player discovery");
			return;
		}
		if(_broadcastReceiver == null){
			_broadcastReceiver = new BroadcastReceiver(){
				@Override
				public void onReceive(Context context, Intent intent) {
					String action = intent.getAction();

					// When discovery finds a device
					if (BluetoothDevice.ACTION_FOUND.equals(action)) {
						// Get the BluetoothDevice object from the Intent
						BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
						// If it's already paired, skip it, because it's been listed already
						//if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
						Player locPlayer = new Player();
						locPlayer.setBluetoothMacAddress(device.getAddress());
						locPlayer.setName(device.getName());
						locPlayer.setPlayerType(EnumPlayerType.BLUETOOTH);
						fireNewPlayerFound(locPlayer);
						//}
					} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

						if(PlayerListActivity.D) Log.i(getClass().getSimpleName(), "Bluetooth player discovery finished");
						context.unregisterReceiver(_broadcastReceiver);
						_broadcastReceiver = null;
						_bluetoothAdapter = null;
					}
				}
			};
			
			

			// Register for broadcasts when a device is discovered
			IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
			parContext.registerReceiver(_broadcastReceiver, filter);


			// Register for broadcasts when discovery has finished
			filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
			parContext.registerReceiver(_broadcastReceiver, filter);
			
			
			// If we're already discovering, stop it
			if (_bluetoothAdapter.isDiscovering()) {
				_bluetoothAdapter.cancelDiscovery();
			}

			// Request discover from BluetoothAdapter
			_bluetoothAdapter.startDiscovery();
			
		}

	}

	public void unregisterResources(final Context parContext){
		if (_bluetoothAdapter.isDiscovering()) {
			_bluetoothAdapter.cancelDiscovery();
		}
		
		parContext.unregisterReceiver(_broadcastReceiver);

	}
	@Override
	public void askPlayerToPlay(Player parPlayer, Game parGame) {
		// TODO Auto-generated method stub

	}

	@Override
	public void handleResponseForPlaying(GameRequestResponse parGameRequestResponse) {
		// TODO Auto-generated method stub

	}
}
